ifeq ($(BUILD_TYPE), shared)
BUILD_TARGET := lib$(LOCAL_TARGET)$(CONFIG_SHARED_LIBRARY_EXTENSION)
BUILD_TARGET_WITH_PATH := $(BUILD_OUTPUT_DIRECTORY)/$(BUILD_TARGET)
BUILD_PREBUILT_FROM :=
endif

ifeq ($(BUILD_TYPE), static)
BUILD_TARGET := lib$(LOCAL_TARGET)$(CONFIG_STATIC_LIBRARY_EXTENSION)
BUILD_TARGET_WITH_PATH := $(BUILD_STATIC_LIBRARIES_DIRECTORY)/$(BUILD_TARGET)
BUILD_PREBUILT_FROM :=
endif

ifeq ($(BUILD_TYPE), executable)
BUILD_TARGET := $(LOCAL_TARGET)
BUILD_TARGET_WITH_PATH := $(BUILD_OUTPUT_DIRECTORY)/$(BUILD_TARGET)
BUILD_PREBUILT_FROM :=
endif

ifeq ($(BUILD_TYPE), prebuilt)
BUILD_TARGET := $(LOCAL_TARGET)
BUILD_TARGET_WITH_PATH := $(BUILD_PREBUILT_LIBRARIES_DIRECTORY)/$(BUILD_TARGET)
# BUILD_TARGET을 생성하기 위해, 복사 할 대상 지정
BUILD_PREBUILT_TARGET := $(strip $(LOCAL_PREBUILT_TARGET))
endif

# 전체 빌드 목록 갱신
BUILD_TARGET_LIST += $(BUILD_TARGET_WITH_PATH)

# 소스 코드 목록
BUILD_C_SOURCE_FILES := $(strip $(addprefix $(LOCAL_PATH)/,$(filter %$(CONFIG_C_EXTENSION),$(LOCAL_SOURCE_FILES))))
BUILD_CXX_SOURCE_FILES := $(strip $(addprefix $(LOCAL_PATH)/,$(filter %$(CONFIG_CXX_EXTENSION),$(LOCAL_SOURCE_FILES))))

# 헤더 참조 경로 목록
BUILD_C_INCLUDES := $(strip $(addprefix -I,$(LOCAL_C_INCLUDES) $(PROJECT_C_INCLUDES)))
BUILD_CXX_INCLUDES := $(strip $(addprefix -I,$(LOCAL_CXX_INCLUDES) $(PROJECT_CXX_INCLUDES)))

# 헤더 목록
BUILD_C_HEADER_FILES := $(strip $(addprefix $(LOCAL_PATH)/,$(filter %$(CONFIG_C_HEADER_EXTENSION),$(LOCAL_HEADER_FILES))))
BUILD_CXX_HEADER_FILES := $(strip $(addprefix $(LOCAL_PATH)/,$(filter %$(CONFIG_CXX_HEADER_EXTENSION),$(LOCAL_HEADER_FILES))))

# 소스 코드에 대한 object 파일 목록
BUILD_C_OBJECT_FILES := $(strip $(addprefix $(BUILD_OBJECTS_DIRECTORY)/,$(patsubst $(BUILD_PROJECTS_DIRECTORY)/%,%,$(BUILD_C_SOURCE_FILES:$(CONFIG_C_EXTENSION)=.o))))
BUILD_CXX_OBJECT_FILES := $(strip $(addprefix $(BUILD_OBJECTS_DIRECTORY)/,$(patsubst $(BUILD_PROJECTS_DIRECTORY)/%,%,$(BUILD_CXX_SOURCE_FILES:$(CONFIG_CXX_EXTENSION)=.o))))

# 소스 코드에 대한 dependency 파일 목록
BUILD_C_DEPEND_FILES := $(strip $(addprefix $(BUILD_OBJECTS_DIRECTORY)/,$(patsubst $(BUILD_PROJECTS_DIRECTORY)/%,%,$(patsubst %$(CONFIG_C_EXTENSION),%.P,$(wildcard $(BUILD_C_SOURCE_FILES))))))
BUILD_CXX_DEPEND_FILES := $(strip $(addprefix $(BUILD_OBJECTS_DIRECTORY)/,$(patsubst $(BUILD_PROJECTS_DIRECTORY)/%,%,$(patsubst %$(CONFIG_CXX_EXTENSION),%.P,$(wildcard $(BUILD_CXX_SOURCE_FILES))))))

BUILD_CFLAGS := $(GLOBAL_CFLAGS_$(BUILD_METHOD)) $(PROJECT_CFLAGS)
BUILD_CXXFLAGS := $(GLOBAL_CXXFLAGS_$(BUILD_METHOD)) $(PROJECT_CXXFLAGS)
BUILD_LDFLAGS := $(GLOBAL_LDFLAGS_$(BUILD_METHOD)) $(PROJECT_LDFLAGS)

# import 된 library 목록
# a b c => liba.a libb.a libc.a
BUILD_STATIC_LIBRARIES := $(strip $(foreach lib,$(LOCAL_STATIC_LIBRARIES),lib$(lib)$(CONFIG_STATIC_LIBRARY_EXTENSION)))
# a b c => liba.so libb.so libc.so
BUILD_SHARED_LIBRARIES := $(strip $(foreach lib,$(LOCAL_SHARED_LIBRARIES),lib$(lib)$(CONFIG_SHARED_LIBRARY_EXTENSION)))
# a b c => -la -lb -lc
BUILD_SHARED_LIBRARIES_TO_LINK := $(strip $(foreach lib,$(LOCAL_SHARED_LIBRARIES),-l$(lib)))
BUILD_PREBUILT_LIBRARIES := $(strip $(LOCAL_PREBUILT_LIBRARIES))

# liba.a libb.a => static/liba.a static/libb.a
BUILD_STATIC_LIBRARIES_WITH_PATH := $(strip $(addprefix $(BUILD_STATIC_LIBRARIES_DIRECTORY)/,$(BUILD_STATIC_LIBRARIES)))
# liba.so libb.so => bin/liba.so bin/libb.so
BUILD_SHARED_LIBRARIES_WITH_PATH := $(strip $(addprefix $(BUILD_OUTPUT_DIRECTORY)/,$(BUILD_SHARED_LIBRARIES)))
# pre.xxx => prebuilt/pre.xxx
BUILD_PREBUILT_LIBRARIES_WITH_PATH := $(strip $(addprefix $(BUILD_PREBUILT_LIBRARIES_DIRECTORY)/,$(BUILD_PREBUILT_LIBRARIES)))

BUILD_TARGET_COUNT := $(call expr_add,$(BUILD_TARGET_COUNT),1)

$(BUILD_TARGET_WITH_PATH) : PRIVATE_CC := $(if $(LOCAL_CC), $(LOCAL_CC), $(GLOBAL_CC))
$(BUILD_TARGET_WITH_PATH) : PRIVATE_CXX := $(if $(LOCAL_CXX), $(LOCAL_CXX), $(GLOBAL_CXX))
$(BUILD_TARGET_WITH_PATH) : PRIVATE_LD := $(if $(LOCAL_LD), $(LOCAL_LD), $(GLOBAL_LD))
$(BUILD_TARGET_WITH_PATH) : PRIVATE_CFLAGS := $(strip $(if $(LOCAL_CFLAGS), $(BUILD_CFLAGS) $(LOCAL_CFLAGS), $(BUILD_CFLAGS)))
$(BUILD_TARGET_WITH_PATH) : PRIVATE_CXXFLAGS := $(strip $(if $(LOCAL_CXXFLAGS), $(BUILD_CXXFLAGS) $(LOCAL_CXXFLAGS), $(BUILD_CXXFLAGS)))
$(BUILD_TARGET_WITH_PATH) : PRIVATE_C_INCLUDES := $(BUILD_C_INCLUDES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_CXX_INCLUDES := $(BUILD_CXX_INCLUDES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_LDFLAGS := $(strip $(if $(LOCAL_LDFLAGS), $(BUILD_LDFLAGS) $(LOCAL_LDFLAGS), $(BUILD_LDFLAGS)))
$(BUILD_TARGET_WITH_PATH) : PRIVATE_STATIC_LIBRARIES := $(LOCAL_STATIC_LIBRARIES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_SHARED_LIBRARIES := $(LOCAL_SHARED_LIBRARIES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_PREBUILT_LIBRARIES := $(LOCAL_PREBUILT_LIBRARIES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_TARGET := $(BUILD_TARGET)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_TARGET_WITH_PATH := $(BUILD_TARGET_WITH_PATH)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_BUILD_TYPE := $(BUILD_TYPE)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_C_SOURCE_FILES := $(BUILD_C_SOURCE_FILES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_CXX_SOURCE_FILES := $(BUILD_CXX_SOURCE_FILES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_C_HEADER_FILES := $(BUILD_C_HEADER_FILES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_CXX_HEADER_FILES := $(BUILD_CXX_HEADER_FILES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_C_OBJECT_FILES := $(BUILD_C_OBJECT_FILES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_CXX_OBJECT_FILES := $(BUILD_CXX_OBJECT_FILES)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_STATIC_LIBRARIES_WITH_PATH := $(BUILD_STATIC_LIBRARIES_WITH_PATH)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_SHARED_LIBRARIES_WITH_PATH := $(BUILD_SHARED_LIBRARIES_WITH_PATH)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_SHARED_LIBRARIES_TO_LINK := $(BUILD_SHARED_LIBRARIES_TO_LINK)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_PREBUILT_LIBRARIES_WITH_PATH := $(BUILD_PREBUILT_LIBRARIES_WITH_PATH)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_TARGET_INDEX := $(BUILD_TARGET_COUNT)
$(BUILD_TARGET_WITH_PATH) : PRIVATE_LOCAL_PATH := $(strip $(LOCAL_PATH))
$(BUILD_TARGET_WITH_PATH) : PRIVATE_PREBUILT_TARGET := $(BUILD_PREBUILT_TARGET)

ifeq ($(BUILD_TYPE), static)
# static일 경우 object 목록을 기록 해 놓는다
BUILD_TARGET_OBJECTS_$(LOCAL_TARGET) := $(strip $(BUILD_C_OBJECT_FILES) $(BUILD_CXX_OBJECT_FILES))
endif
